package svgedit.TestSVG;

import static org.junit.Assert.assertTrue;
import org.junit.Test;
import svgedit.gui.Frame;
import svgedit.svg.SVGDocument;
import svgedit.svg.SVGGroup;
import svgedit.svg.SVGLineElement;
import svgundoredo.UngroupElementCommand;

/**
 * test UngroupElement class
 */

public class UngroupElementCommandTest
{
    /**
     * test execute() method
     */
    @Test
    public void testexecute()
    {
        // create new frame
        Frame frame = new Frame();
        Frame f = new Frame();
        SVGDocument d = f.getDocument();
        SVGDocument document;
        document = frame.getDocument();
        SVGGroup group = new SVGGroup(document);
        SVGLineElement element = new SVGLineElement(d);
        group.appendChild(element);
        group.appendChild(element);
        group.appendChild(element);
        group.appendChild(element);
        // check the group
        assertTrue(f.getView().getSelectedElements().length == 0);
        assertTrue(group.getChildren().length == 4);
        UngroupElementCommand ug = new UngroupElementCommand(group, f);
        ug.execute();
        // ungrouped
        assertTrue(f.getView().getSelectedElements().length == 4);
        assertTrue(group.getChildren().length == 3);
        
    }
    
    /**
     * test undo() method
     */
    @Test
    public void testundo()
    {
        // create new frame
        Frame frame = new Frame();
        Frame f = new Frame();
        SVGDocument d = f.getDocument();
        SVGDocument document;
        document = frame.getDocument();
        SVGGroup group = new SVGGroup(document);
        SVGLineElement element = new SVGLineElement(d);
        group.appendChild(element);
        group.appendChild(element);
        group.appendChild(element);
        group.appendChild(element);
        assertTrue(f.getView().getSelectedElements().length == 0);
        assertTrue(group.getChildren().length == 4);
        UngroupElementCommand ug = new UngroupElementCommand(group, f);
        ug.execute();
        // ungrouped
        assertTrue(f.getView().getSelectedElements().length == 4);
        assertTrue(group.getChildren().length == 3);
        ug.undo();
        assertTrue(f.getView().getSelectedElements().length == 4);
        assertTrue(group.getChildren().length == 4);
        
    }
}
